package cn.hl.hm.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hl.hm.dao.HotDetailMapper;
import cn.hl.hm.dao.HotMapper;
import cn.hl.hm.dao.UserMapper;
import cn.hl.hm.dao.UserRouteMapper;
import cn.hl.hm.entity.Hot;
import cn.hl.hm.entity.HotDetail;
import cn.hl.hm.entity.User;
import cn.hl.hm.entity.UserRoute;
import cn.hl.hm.service.UserService;
@Service
public class UserServiceImpl implements UserService{
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private HotDetailMapper hotDetailMapper;
	@Autowired
	private HotMapper hotMapper;
	
	@Autowired
	private UserRouteMapper userRouteMapper;
	@Override
	public List<User> selectUser(User user) {
		return userMapper.selectUser(user);
	}
	/**
	* @Title: addHotRecord
	* @author: 大和
	* @date :2016年9月13日
	* @Description: TODO
	* @throws
	*/
	@Transactional
	public int addHotRecord(Integer userId, String role, String oper) {
		int i = 0;
		Date date = new Date();
		String now = new SimpleDateFormat("yyyy-MM-dd").format(date);
		HotDetail thd = new HotDetail();
		thd.setCreateTime(date);
		thd.setType(role);
		thd.setOperate(oper);
		thd.setUserId(userId);
		i = hotDetailMapper.insertSelective(thd);

		Hot hot = hotMapper.selectByTime(now);
		if (hot != null) {
			if (role.equals("1")) {//用户
				hot.setUserAmount(hot.getUserAmount() + 1);

			} else {//司机
				hot.setDriverAmount(hot.getDriverAmount() + 1);
			}
			// 更新+1
			hot.setUpdateTime(date);
			i = hotMapper.updateByPrimaryKeySelective(hot);
		} else {
			Hot record = new Hot();
			if (role.equals("1")) {//用户
				record.setDriverAmount(0);
				record.setUserAmount(1);

			} else {//司机
				record.setUserAmount(0);
				record.setDriverAmount(1);

			}
			record.setUpdateTime(date);
			record.setCreateTime(date);
			i = hotMapper.insert(record);
		}
		return i;

	}
	@Override
	@Transactional
	public int add(User user) {
		return userMapper.insertSelective(user);
	}
	@Override
	public int updateUser(User user) {
		return userMapper.updateByPrimaryKeySelective(user);
	}
	@Override
	public  List<UserRoute>  selectUserRoute(UserRoute userRoute) {
		return userRouteMapper.selectUserRoute(userRoute);
	}
	@Override
	public int addUserRoute(UserRoute userRoute) {
		// TODO Auto-generated method stub
		return userRouteMapper.insertSelective(userRoute);
	}
}
